const env = require('./OssConfig.js');

function send_photo(data, successfun) {

  wx.request({
    url: "http://127.0.0.1:7001/oss/policy",
    method: 'POST',
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      console.log("从后台获取数据成功"+res.data.data)
      console.log(res.data)
      if (res.data.code == 0) {
        for (let i = 0; i < data.length; i++) {
          console.log("data长度=" + data.length)
          console.log(data)
        
          // let fileTypeIndex = photo.lastIndexOf('.');
          // let fileType = photo.substring(fileTypeIndex);

          //阿里云账户信息
          const aliyunFileKey = res.data.data.dir + data;
          const aliyunServerURL = env.uploadImageUrl;//OSS地址，需要https
          const accessid = env.OSSAccessKeyId;
          const policyBase64 = res.data.data.policy;
          const signature = res.data.data.signature;
          console.log( aliyunServerURL)
          wx.uploadFile({
            url: "https://jdyy.oss-cn-beijing.aliyuncs.com", //仅为示例，非真实的接口地址 自己写映射你Java接口的路径
            filePath: data[i] + "",
            name: 'file',
            formData: {
              'key': aliyunFileKey,
              'policy': policyBase64,
              'OSSAccessKeyId': accessid,
              'signature': signature,
              'success_action_status': '200',
            },
            header: {
              "Content-Type": "multipart/form-data",
              'accept': 'application/json',
              'Authorization': 'Bearer ..' //若有token，此处换上你的token，没有的话省略
            },
            success: function (res) {    
              console.log("文件http地址：" + res.data)
              console.log(aliyunServerURL + aliyunFileKey)
              console.log(res)
              if(res.statusCode==200){
              wx.showModal({
                title: '上传到oss成功',
                content: "成功",
                showCancel: false
              });
              }else{
                wx.showModal({
                  title: '上传到osss失败',
                  content: "失败",
                  showCancel: false
                });
              }
            }
          })
        }
      } else {
        wx.showModal({
          title: '后台获取数据错误',
          content: "失败",
          showCancel: false
        });
      }
    }
  });

  
}

module.exports = {
  send_photo: send_photo
}